import wx from 'wx'
import Fly from 'flyio'

const request = new Fly()

// 设置请求基地址
// request.config.baseURL = "http://127.0.0.1:8360/"

request.interceptors.request.use((request) => {
  // 给所有请求添加自定义header，带上token信息让服务器验证用户登陆
  request.headers['token'] = wx.getStorageSync('token');
  request.headers['apikey'] = '03892de79f9611e8b36c00163e0e500c';
  request.headers['Content-Type'] = 'application/x-www-form-urlencoded';
  // request.data['token'] = 'AGFQYVM1U2cAMQ9sXTEOPQ5oVjcBfAA/AzUCMlppUT9QLFBlBGdXZgY2CGUNfQgzBGYDYVE1BTsFLgA2V3hXOgB8UGVTMlNgACoPf10rDi4Oa1YxAWEAPg==';
  // apikey: '03892de79f9611e8b36c00163e0e500c';
  // token: 'AGFQYVM1U2cAMQ9sXTEOPQ5oVjcBfAA/AzUCMlppUT9QLFBlBGdXZgY2CGUNfQgzBGYDYVE1BTsFLgA2V3hXOgB8UGVTMlNgACoPf10rDi4Oa1YxAWEAPg==';
  // token: 'AGFQYVM1U2cAMQ9sXTEOPQ5oVjcBfAA/AzUCMlppUT9QLFBlBGdXZgY2CGUNfQgzBGYDYVE1BTsFLgA2V3hXOgB8UGVTMlNgACoPf10rDi4Oa1YxAWEAPg==',

  // console.log('flyio发请求,request为', request);
  wx.showNavigationBarLoading()
  // console.log(request.body)
  let myToken=''
  if( wx.getStorageSync('token')){
    myToken=wx.getStorageSync('token');
  }else{
    myToken='A2IDMlI0BzICMQljAG4BOVIyBGJTLlRrU2UCMgc0BGpSLlZjCGsGN1VlCGVYKAE6D21SMFM3BzkPJFBmWnUCawN/AzZSMwc0AigJeQB2ASFSNwRjUzNUag=='
  }
  request.body.token=myToken;
  request.body.apikey='03892de79f9611e8b36c00163e0e500c' ;
 
  return request
})

request.interceptors.response.use(
  (response, promise) => {
    wx.hideNavigationBarLoading()
    // console.log(promise,response)
    return promise.resolve(response.data)
  },
  (err, promise) => {
    wx.hideNavigationBarLoading()
    wx.showToast({
      title: err.message,
      icon: 'none'
    })
    return promise.resolve()
  }
)

export default request
